﻿/*-----------------------------------------------------------------------------!
! CustomerKazImpl class                                                        !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : CustomerKazImpl.cs                                                !
! Function : Implementation of ICustomerKazImpl interface                      !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
! Last changed by: Md. Shaiful Islam, May 08 2011                              !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Sadharon;
using Model.Upadan;
using System.Data.SqlClient;
using Model.Sohaok;
using System.Data;

namespace Model.Poribar.CustomerPanjeri
{
    class CustomerKazImpl : ICustomerKaz
    {

        #region CustomerKazImpl Members: Add Method
        /**
         * add : added new customer
         * @param object
         * @return Result object
         **/
        public Result add(object _obj)
        {
            Result result = new Result();
            Customer customer = new Customer();
            DBSadharon dbUtil = new DBSadharon();
            customer = (Customer)_obj;

            SqlConnection conn = new SqlConnection();
            try
            {
                conn = DatabaseConnection.GetConnection();
                // get UniqId for customer
                string uniqId = dbUtil.getUniqId(conn, "Customer", "CU");
                if (uniqId.Equals("falied"))
                {
                    string msg = "Failed to add new customer: Error in generating uniqeId. If problem persist " +
                                     "please contact with system administrator";
                    result = Util.setSuccessResult(false, msg);
                }
                else
                {
                    customer.Id = uniqId;
                    conn.Open();
                    string query = "Insert into Customer (Id, Reference, CompanyName, ContactPerson, SaleTypeId, " +
                        "Live) Values(" +
                        "'" + Util.filterString(customer.Id) + "'," +
                        "'" + Util.filterString(customer.AccountReference) + "'," +
                        "'" + Util.filterString(customer.CompanyName) + "'," +
                        "'" + Util.filterString(customer.ContactPerson) + "'," +
                        "'" + Util.filterString(customer.SaleTypeId) + "'," +
                        "'" + "L" + "'" +
                        ")";
                    
                    if (DBSadharon.MakeQuery(query,conn).ExecuteNonQuery() > 0)
                    {
                        result = Util.setSuccessResult(true, "New customer added successfully", customer);
                    }
                    else
                    {
                        result = Util.setSuccessResult(false, "Failed to add (Query failed)");
                    }
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }

        #endregion

        #region CustomerKazImpl member: getById
        ///
        // getById
        // @param personId
        // @return result object
        ///
        public Result getById(string id)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from Customer Where Id='" + id + "' and Live='L'";
                SqlDataReader customerDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (customerDataReader.HasRows)
                {
                    Customer customer = new Customer();
                    while (customerDataReader.Read())
                    {
                        customer.Id = customerDataReader["Id"].ToString();
                        customer.AccountReference = customerDataReader["Reference"].ToString();
                        customer.CompanyName = customerDataReader["CompanyName"].ToString();
                        customer.ContactPerson = customerDataReader["ContactPerson"].ToString();
                        customer.Live = customerDataReader["Live"].ToString();
                        customer.SaleTypeId = customerDataReader["SaleTypeId"].ToString();                        
                    }
                    result = Util.setSuccessResult(true, "Successful", customer);
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get customer details: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get customer details: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region CustomerKazImpl member: getList
        ///
        // getList
        ///
        public Result getList()
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from Customer Where Live='L'";
                SqlDataReader customerDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (customerDataReader.HasRows)
                {
                    List<Customer> listCustomer = new List<Customer>();
                    Customer customer = new Customer();
                    while (customerDataReader.Read())
                    {
                        customer = new Customer();
                        customer.Id = customerDataReader["Id"].ToString();
                        customer.AccountReference = customerDataReader["Reference"].ToString();
                        customer.CompanyName = customerDataReader["CompanyName"].ToString();
                        customer.ContactPerson = customerDataReader["ContactPerson"].ToString();
                        customer.Live = customerDataReader["Live"].ToString();
                        customer.SaleTypeId = customerDataReader["SaleTypeId"].ToString(); 
                        listCustomer.Add(customer);
                    }
                    result = Util.setSuccessResult(true, "Successful", listCustomer );
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get customer list: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get customer list: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region CustomerKazImpl Members: Update Method
        /**
         * update : update customer information
         * @param object
         * @return Result object
         **/
        public Result update(object _obj)
        {
            Result result = new Result();
            Customer customer = new Customer();
            customer = (Customer)_obj;
            customer.Live = "L";

            SqlConnection conn = new SqlConnection();
            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Update Customer set " +
                    "Reference='" + Util.filterString(customer.AccountReference) + "', " +
                    "CompanyName='" + Util.filterString(customer.CompanyName) + "', " +
                    "ContactPerson='" + Util.filterString(customer.ContactPerson) + "'," +
                    "SaleTypeId='" + Util.filterString(customer.SaleTypeId) + "', " +
                    "Live='" + Util.filterString(customer.Live) + "' " +
                    "Where Id='" + customer.Id + "'";

                if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "Customer updated successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to update customer (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to update customer: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to update customer: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region CustomerKazImpl Members: delete Method
        /**
         * delete : delete customer information
         * @param id
         * @return Result object
         **/
        public Result delete(string id)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Update Customer set Live='D' Where Id='" + id + "'";

                if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "customer deleted successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to delete customer (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete customer: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete customer: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

    }
}
